package com.liuxin.zhiyuan.shujuzhongxin.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liuxin.common.utils.PageUtils;
import com.liuxin.common.utils.Query;
import com.liuxin.zhiyuan.shujuzhongxin.domain.dto.RecommendationItemDTO;
import com.liuxin.zhiyuan.shujuzhongxin.domain.dto.RecruitMajorDTO;
import com.liuxin.zhiyuan.shujuzhongxin.domain.po.MajorGroupScoreEntity;
import com.liuxin.zhiyuan.shujuzhongxin.domain.po.ScoreSegmentEntity;
import com.liuxin.zhiyuan.shujuzhongxin.domain.po.XueyuanInfoEntity;
import com.liuxin.zhiyuan.shujuzhongxin.mapper.MajorGroupScoreMapper;
import com.liuxin.zhiyuan.shujuzhongxin.mapper.RecruitMajorInfoMapper;
import com.liuxin.zhiyuan.shujuzhongxin.service.MajorGroupScoreService;
import com.liuxin.zhiyuan.shujuzhongxin.service.ScoreSegmentService;
import com.liuxin.zhiyuan.shujuzhongxin.service.XueyuanInfoService;
import com.liuxin.zhiyuan.shujuzhongxin.service.YuanxiaoinfoService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MajorGroupScoreServiceImpl extends ServiceImpl<MajorGroupScoreMapper, MajorGroupScoreEntity> implements MajorGroupScoreService {


    @Autowired
    private XueyuanInfoService xueyuanInfoService;

    @Autowired
    private YuanxiaoinfoService yuanxiaoinfoService;

    @Autowired
    private RecruitMajorInfoMapper recruitMajorInfoMapper;

    @Autowired
    private ScoreSegmentService scoreSegmentService;



    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MajorGroupScoreEntity> page = this.page(
                new Query<MajorGroupScoreEntity>().getPage(params),
                new QueryWrapper<MajorGroupScoreEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public List<RecommendationItemDTO> getMajorGroupsByRankRange(Integer minRank, Integer maxRank, Integer natureId, String province,String year, Integer limit) {
        Map<String, Object> params = new HashMap<>();
        params.put("minRank", minRank);
        params.put("maxRank", maxRank);
        params.put("natureId", natureId);
        params.put("year", year);
        params.put("province", province);
        params.put("limit", limit);
        List<RecommendationItemDTO> recommendationItemResult = baseMapper.queryByRankAndSubject(params);
        recommendationItemResult.forEach(item -> {
            String nature = item.getNature();
            Integer schoolId = item.getSchoolId();
            String schoolCode = item.getSchoolMajorCode();
            List<RecruitMajorDTO> recruitMajorList = recruitMajorInfoMapper.queryRecruitMajorList(schoolId, nature, schoolCode);
            item.setRecruitMajorList(recruitMajorList);
        });

        return recommendationItemResult;
    }

    @Override
    public Map<String, Object> getVolunteerRecommendation(Integer studentId) {
        Map<String, Object> result = new HashMap<>();
        // 1. 获取学生信息
        XueyuanInfoEntity student = xueyuanInfoService.getById(studentId);
        if (student == null) {
            result.put("error", "学生信息不存在");
            return result;
        }
        Integer examRank = student.getExamRank();
        String firstSubject = student.getFirstSubject();
        String province = student.getExamProvince();
        if (examRank == null) {
            result.put("error", "学生位次信息缺失");
            return result;
        }
        if(StringUtils.isBlank(firstSubject)){
            result.put("error", "学生第一志愿信息缺失");
            return result;
        }
        int natureId;
        if("物理".equals(firstSubject)){
            natureId = 4;
        }else{
            natureId=5;
        }
        String year = "";
        if(student.getYear() == null || "".equals(student.getYear())){
            result.put("error", "学生高考年份缺失");
            return result;
        }
        year = student.getYear();
        Integer examScore = student.getExamScore();
        // 2. 计算稳冲保的位次范围
        Integer paiming = scoreSegmentService.findPaimingByScore(year, firstSubject, examScore-7);
        System.out.println("排名===================="+paiming);
        // 冲：当前位次 -10% 到 -5%
        int rushMinRank = scoreSegmentService.findPaimingByScore(year, firstSubject, examScore);
        int rushMaxRank = scoreSegmentService.findPaimingByScore(year, firstSubject, examScore-7);
        // 稳：当前位次 ± 5%
        int stableMinRank = scoreSegmentService.findPaimingByScore(year, firstSubject, examScore-7);
        int stableMaxRank = scoreSegmentService.findPaimingByScore(year, firstSubject, examScore-13);
        // 保：当前位次 +5% 到 +10%
        int safeMinRank = scoreSegmentService.findPaimingByScore(year, firstSubject, examScore-13);
        int safeMaxRank = scoreSegmentService.findPaimingByScore(year, firstSubject, 200);

        // 3. 分别查询稳冲保的专业组
        List<RecommendationItemDTO> stableGroups = getMajorGroupsByRankRange(
                stableMinRank, stableMaxRank, natureId, province,year, 16);
        List<RecommendationItemDTO> rushGroups = getMajorGroupsByRankRange(
                rushMinRank, rushMaxRank, natureId, province,year, 16);
        List<RecommendationItemDTO> safeGroups = getMajorGroupsByRankRange(
                safeMinRank, safeMaxRank, natureId, province,year, 16);

        // 4. 构建返回结果
        result.put("studentInfo", student);
        result.put("rankRanges", new HashMap<String, Object>() {{
            put("stable", stableMinRank + "-" + stableMaxRank);
            put("rush", rushMinRank + "-" + rushMaxRank);
            put("safe", safeMinRank + "-" + safeMaxRank);
        }});
        result.put("stableRecommendations", stableGroups);
        result.put("rushRecommendations", rushGroups);
        result.put("safeRecommendations", safeGroups);

        return result;
    }

}
